<?php

namespace Packages\Log\Services;

use App\Tools\Common;
use Packages\Log\Models\OperationModel;
use Illuminate\Support\Facades\DB;

class LogService
{

    /**
     * 获取列表
     * @param $data
     * @return array
     */
    public function list($data)
    {
        $current = $data['current'] ?? 1;
        $pageSize = $data['pageSize'] ?? 10;

        // 查询条件
        $paginate = OperationModel::select('*')
            ->when(@$data['app_id'], function ($q) use ($data) {
                $q->where('app_id', $data['app_id']);
            })
            ->when(@$data['user_id'], function ($q) use ($data) {
                $q->where('user_id', $data['user_id']);
            })
            ->when(@$data['path'], function ($q) use ($data) {
                $q->where('path', $data['path']);
            })
            ->when(@$data['method'], function ($q) use ($data) {
                $q->where('method', $data['method']);
            })
            ->when(@$data['ip'], function ($q) use ($data) {
                $q->where('ip', $data['ip']);
            })
            ->when(@$data['url'], function ($q) use ($data) {
                $q->where('url', $data['url']);
            })
            ->when(@$data['obj_id'], function ($q) use ($data) {
                $q->where('obj_id', $data['obj_id']);
            })
            ->when(@$data['log_time'], function ($q) use ($data) {
                $q->whereBetween('log_time', $data['log_time']);
            })
            ->orderBy('log_time', 'desc')
            ->paginate($pageSize, ['*'], 'page', $current);

        return [
            'status' => true,
            'data' => [
                'current' => $current,
                'data' => $paginate->items(),
                'pageSize' => $pageSize,
                'total' => $paginate->total(),
            ]
        ];
    }

    /**
     * 添加
     * @param $data
     * @return array
     */
    public function add($data)
    {

        $result = OperationModel::create([
            'guid' => Common::getUuid(),
            'app_id' => $data['app_id'] ?? '0',
            'user_id' => $data['user_id'],
            'path' => $data['path'],
            'method' => $data['method'],
            'ip' => $data['ip'],
            'request' => $data['request'],
            'response' => $data['response'],
            'url' => $data['url'],
            'obj_id' => $data['obj_id'],
            'log_time' => $data['log_time']
        ]);

        if (!$result) {
            return ['status' => false, 'data' => '添加失败!', 'code' => 41044];
        }

        return ['status' => true, 'data' => $result];
    }

    /**
     * 详情
     * @param $guid
     * @return array
     */
    public function detail($guid)
    {

        // 查询详情
        $result = OperationModel::where('guid', $guid)
            ->first();

        if (!$result) {
            return ['status' => false, 'data' => '数据不存在!', 'code' => 41044];
        }

        return ['status' => true, 'data' => $result];
    }
}
